import java.io.*;

class MatLabArray
{
    double[] data;
    String name = "";
    int length = 0;

    public MatLabArray(String aName, double[] cells)
    {
        this.data = cells;
        this.name = aName;
        this.length = this.data.length;
    }

    public MatLabArray(String aName, double[] cells, int len)
    {
        this.data = cells;
        this.name = aName;
        this.length = len;
    }

    public void print()
    {
        try
        {
            System.out.print(this.name + " = ["+this.data[0]);

            for (int i = 1; i < this.length; i++)
            {
                System.out.print(", " + this.data[i]);
            }

            System.out.println("]");
        }
        catch (Exception e)
        {
            System.err.println ("Error writing array to specified file.");
        }
    }

    public void toFile(String filename)
    {
        toFile(filename,false);
    }

     public void toFile(String filename, boolean append)
    {
        FileOutputStream out;
        PrintStream p;
        try
        {
                out = new FileOutputStream(filename, append);
                p = new PrintStream( out );
                p.print(this.name + " = ["+this.data[0]);

                for (int i = 1; i < this.length; i++)
                {
                    p.print(", " + this.data[i]);
                }

                p.println("];");

                p.close();
        }
        catch (Exception e)
        {
            System.err.println ("Error writing array to specified file.");
        }
    }

}
